package top.lunarye.modules.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.cache.annotation.*;
import top.lunarye.modules.staff.entity.Role;
import top.lunarye.modules.staff.mapper.RoleMapper;
import top.lunarye.modules.staff.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lunar
 * @since 2021-07-13
 */
@Service
@CacheConfig(cacheNames = "role")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Override
    @Cacheable(value = "role-list", key="'list'", unless="#result == null")
    public List<Role> list() {
        return super.list();
    }

    @Override
    @Cacheable(key="'entity'+#id", unless="#result == null")
    public Role getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    @CacheEvict(value="role-list", key="'list'")
    public boolean save(Role entity) {
        return super.save(entity);
    }

    /**
     * 一次性触发多个缓存操作
     * @param id
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value="role-list", key="'list'"),
            @CacheEvict(key="'entity'+#id")
    })
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value="role-list", key="'list'"),
            @CacheEvict(key="'entity'+#entity.id")
    })
    public boolean updateById(Role entity) {
        return super.updateById(entity);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value="role-list", key="'list'"),
            @CacheEvict(key="'entity'+#entity.id")
    })
    public boolean saveOrUpdate(Role entity) {
        return super.saveOrUpdate(entity);
    }

    @Override
    public List<Role> selectAndPage(Page<Role> page, Wrapper<Role> queryWrapper) {
        return super.page(page, queryWrapper).getRecords();
    }
}
